<?php

namespace Org\Util;

class ArrayToolkit
{
    public static function column(array $array, $columnName)
    {
        if (empty($array)) {
            return array();
        }

        $column = array();

        foreach ($array as $item) {
            if (isset($item[$columnName])) {
                $column[] = $item[$columnName];
            }
        }

        return $column;
    }

    public static function parts(array $array, array $keys)
    {
        foreach (array_keys($array) as $key) {
            if (!in_array($key, $keys)) {
                unset($array[$key]);
            }
        }

        return $array;
    }

    public static function requireds(array $array, array $keys)
    {
        foreach ($keys as $key) {
            if (!array_key_exists($key, $array)) {
                return false;
            }
        }

        return true;
    }

    public static function changes(array $before, array $after)
    {
        $changes = array('before' => array(), 'after' => array());

        foreach ($after as $key => $value) {
            if (!isset($before[$key])) {
                continue;
            }

            if ($value != $before[$key]) {
                $changes['before'][$key] = $before[$key];
                $changes['after'][$key] = $value;
            }
        }

        return $changes;
    }

    public static function group(array $array, $key)
    {
        $grouped = array();

        foreach ($array as $item) {
            if (empty($grouped[$item[$key]])) {
                $grouped[$item[$key]] = array();
            }

            $grouped[$item[$key]][] = $item;
        }

        return $grouped;
    }

    public static function index(array $array, $name)  //将字段转为数组名KEY
    {
        $indexedArray = array();

        if (empty($array)) {
            return $indexedArray;
        }

        foreach ($array as $item) {
            if (isset($item[$name])) {
                $indexedArray[$item[$name]] = $item;
                continue;
            }
        }

        return $indexedArray;
    }

    public static function rename(array $array, array $map)
    {
        $keys = array_keys($map);

        foreach ($array as $key => $value) {
            if (in_array($key, $keys)) {
                $array[$map[$key]] = $value;
                unset($array[$key]);
            }
        }

        return $array;
    }

    public static function filter(array $array, array $specialValues)
    {
        $filtered = array();

        foreach ($specialValues as $key => $value) {
            if (!array_key_exists($key, $array)) {
                continue;
            }

            if (is_array($value)) {
                $filtered[$key] = (array)$array[$key];
            } elseif (is_int($value)) {
                $filtered[$key] = (int)$array[$key];
            } elseif (is_float($value)) {
                $filtered[$key] = (float)$array[$key];
            } elseif (is_bool($value)) {
                $filtered[$key] = (bool)$array[$key];
            } else {
                $filtered[$key] = (string)$array[$key];
            }

            if (empty($filtered[$key])) {
                $filtered[$key] = $value;
            }
        }

        return $filtered;
    }

    public static function trim($array, $charlist = null)
    {
        if (!is_array($array)) {
            return $array;
        }

        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $array[$key] = ArrayToolkitBehavior::trim($value);
            } elseif (is_string($value)) {
                if ($charlist) {
                    $array[$key] = trim($value, $charlist);
                } else {
                    $array[$key] = trim($value);
                }
            }
        }

        return $array;
    }

    public static function every($array, $callback = null)
    {
        foreach ($array as $value) {
            if (is_null($callback) && !$value) {
                return false;
            } else if (is_callable($callback) && !$callback($value)) {
                return false;
            }
        }
        return true;
    }

    public static function some($array, $callback = null)
    {
        foreach ($array as $value) {
            if (is_null($callback) && $value) {
                return true;
            } else if (is_callable($callback) && $callback($value)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 取数据字段值
     * @param $field
     * @param $data
     */
    public static function field($field, $data)
    {
        $tmp = array();
        foreach ($data as $k => $v) {
            if (in_array($k, $field)) {
                $tmp[$k] = $v;
            }
        }
        return $tmp;
    }

    /**
     * 转换为键值数组
     * @param $data
     * @param $keyField
     * @param $valueField
     * @return array
     */
    public static function KeyAndValue($data, $keyField, $valueField)
    {
        $tmp = array();
        foreach ($data as $key => $row) {
            if (isset($row[$keyField])) {
                $tmp[$row[$keyField]] = $row[$valueField];
            } else {
                $tmp[$keyField] = $row[$valueField];
            }
        }
        return $tmp;
    }


    //将一维数组转换为二维数组，二维数组保存不变
    public static function arrayToarr(array $array)
    {
        $newarray = array();
        foreach ($array as $key => $a) {
            if (!is_array($a)) {
                $newarray[$key][] = $a;
            } else {
                $newarray[$key] = $a;
            }
        }
        return $newarray;
    }

    //json多维数组转
    public static function jsonDecodeArr(array $array, array $columnName)
    {
        if (empty($array)) {
            return array();
        }

        foreach ($columnName as $keys => $name) {
            if (isset($name)) {
                foreach ($array as $key => $item) {
                    if (isset($item[$name])) {
                        $array[$key][$name] = json_decode($item[$name], true);
                    }
                }
            }
        }
        return $array;
    }

    //JSON一维数组转json
    public static function jsonDecode(array $array, array $columnName)
    {
        if (empty($array)) {
            return array();
        }

        foreach ($columnName as $keys => $name) {
            if (isset($name)) {
                foreach ($array as $key => $item) {
                    if ($key == $name) {
                        $array[$key] = json_decode($item, true);
                    }
                }
            }
        }
        return $array;
    }

    //打乱二维数组显示顺序，随机
    public static function shuffle_assoc($list)
    {
        if (!is_array($list)) return $list;
        $keys = array_keys($list);
        shuffle($keys);
        $random = array();
        foreach ($keys as $key) {
            $random[$key] = $list[$key];
        }
        return $random;
    }

    /**
     * 将包含数组的字段转换JSON格式(non-PHPdoc)
     * @see \Common\Behavior\BaseBehavior::parseIsArray()
     */
    public static function parseIsArray($data)
    {
        if (empty($data) && !is_array($data)) {
            return false;
        }
        foreach ($data as $key => $v) {
            if (is_array($v)) {
                $data[$key] = json_encode($v);
            }

            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $v)) {
                $data[$key] = D('UploadFiles')->base64ImageUpload($v);
            }
        }
        return $data;
    }

    /**
     * 把数组的key值恢复成类似于0,1,2,3,4,5
     * @param unknown $arr
     * @return unknown|multitype:NULL unknown
     */
    public static function restore_array($arr)
    {
        if (!is_array($arr)) {
            return $arr;
        }
        $c = 0;
        $new = array();
        while (list($key, $value) = each($arr)) {
            if (is_array($value)) {
                $new[$c] = restore_array($value);
            } else {
                $new[$c] = $value;
            }
            $c++;
        }
        return $new;
    }

    /**
     * 去掉数组中两边的空格
     * @param unknown $Input
     * @return string
     */
    public static function TrimArray($arr)
    {
        if (!is_array($arr)) {
            return $arr;
        }

        while (list($key, $value) = each($arr)) {
            if (is_array($value)) {
                $arr[$key] = TrimArray($value);
            } else {
                $arr[$key] = trim($value);
            }
        }
        return $arr;

    }

    //json多维数组转
    public static function josnDecode(array $array, array $columnName)
    {
        if (empty($array)) {
            return array();
        }

        foreach ($columnName as $keys => $name) {
            if (isset($name)) {
                foreach ($array as $key => $item) {
                    if (isset($item[$name])) {
                        $array[$key][$name] = json_decode($item[$name], true);
                    }
                }
            }
        }
        return $array;
    }

    /**
     * 二维数组指定字段排序
     * @param $list 数组
     * @param $field 排序字段
     * @param int $sort 排序方式
     * @return mixed
     */
    public function arrayMutilSort(&$list, $field, $sort = SORT_ASC)
    {
        array_multisort(array_column($list, $field), $sort, $list);
        return $list;
    }

    public static function str2array()
    {
    }

}
